package com.qy127.javaweb.controller;


import com.qy127.javaweb.dao.impl.DeptDaoImpl;
import com.qy127.javaweb.entity.Dept;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/dept/*")
public class DeptServlet extends HttpServlet {
    DeptDaoImpl deptDao = new DeptDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        int i = uri.lastIndexOf("/");
        String s = uri.substring(i + 1);
        switch (s){
            case "seldept":
                seldept(req,resp);
                break;
            case "insdept":
                insdept(req,resp);
                break;
            case "deldept":
                deldept(req,resp);
                break;
            case "goupdatedept":
                goupdatedept(req,resp);
                break;
            case "updatedept":
                updatedept(req,resp);
                break;
            default:
                break;
        }
    }

    /**
     * 查询所有部门数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void seldept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chsrset=utf-8");
        //数据操作
        DeptDaoImpl deptDao = new DeptDaoImpl();
        List<Dept> allDept = deptDao.getAllDept();
        //转发
        req.setAttribute("allDept",allDept);
        req.getRequestDispatcher("/dept.jsp").forward(req,resp);
    }

    /**
     * 增加一个部门
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insdept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chsrset=utf-8");
        //数据操作(请求参数)
        String deptNo = req.getParameter("deptNo");
        String dname = req.getParameter("dname");
        String remark = req.getParameter("remark");
        String status = req.getParameter("status");
        //数据操作
        int i = deptDao.insertOneDept(Integer.parseInt(deptNo), dname, remark, Integer.parseInt(status));
        if (i>0){
            req.setAttribute("msg","添加成功");
        }else {
            req.setAttribute("msg","添加失败");
        }
        req.getRequestDispatcher("/adddeptb.jsp").forward(req,resp);
    }
    /**
     * 删除一个部门数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deldept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chsrset=utf-8");
        //数据操作
        String id = req.getParameter("id");
        int i = deptDao.deleteOneDept(Integer.parseInt(id));
        System.out.println(i);
        seldept(req,resp);

    }

    /**
     * 去修改信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void goupdatedept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chsrset=utf-8");
        //数据操作
        String id = req.getParameter("id");
        Dept dept = deptDao.goupdateDept(Integer.parseInt(id));
        req.setAttribute("dept",dept);
        req.getRequestDispatcher("/updatedept.jsp").forward(req,resp);

    }

    /**
     * 修改
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updatedept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chsrset=utf-8");
        //数据操作
        String deptid = req.getParameter("id");
        String deptNo = req.getParameter("deptNo");
        String dname = req.getParameter("dname");
        String remark = req.getParameter("remark");
        String status = req.getParameter("status");
        //数据操作
        Dept dept = new Dept(Integer.parseInt(deptid),Integer.parseInt(deptNo), dname, remark, Integer.parseInt(status));
        int i = deptDao.updatedept(dept);
        System.out.println(i);
        if (i>0){
            seldept(req,resp);
        }

    }

}
